/**
 * Filename:    StringHelper.java
 * Project:     tudoulady
 */
package com.zhisheng.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 
 * @since 2008-6-11
 */
public class StringUtils {

	public static String multiLineCut(String src, int line1Length,
			int line2Length) {
		return multiLineCut0(src, line1Length, line2Length);
	}

	public static String multiLineCut(String src, int line1Length,
			int line2Length, int line3Length) {
		return multiLineCut0(src, line1Length, line2Length, line3Length);
	}

	private static String multiLineCut0(String src, int... byteLengths) {
		String tmp = src;
		StringBuilder builder = new StringBuilder(src.length());
		for (int i = 0; i < byteLengths.length - 1; i++) {
			String newStr = subString(tmp, byteLengths[i], false);
			builder.append(newStr);
			tmp = tmp.substring(newStr.length());
			if (tmp.length() == 0) {
				return builder.toString();
			}
			builder.append("<br>");
		}

		builder
				.append(subString(tmp, byteLengths[byteLengths.length - 1],
						true));

		return builder.toString();
	}

	public static String subString(String src, int byteLength) {
		return subString(src, byteLength, true);
	}

	/**
	 * return the substring from the string src, and the out is expressed ".."
	 * 
	 * @param src
	 * @param byteLength
	 * @return
	 */
	public static String subString(String src, int byteLength, boolean addDot) {
		if (src == null) {
			return "";
		}

		int index = 0;
		int byteSize = 0;
		for (index = 0; index < src.length(); index++) {
			char ch = src.charAt(index);
			if (isWideByteChar(ch)) {
				byteSize += 2;
			} else {
				byteSize++;
			}

			if (byteSize > byteLength) {
				break;
			}
		}

		if (index >= src.length()) {
			return src;
		}

		if (addDot == false) {
			return src.substring(0, index);
		}

		for (; index > 0; index--) {
			char ch = src.charAt(index);
			if (isWideByteChar(ch)) {
				byteSize -= 2;
			} else {
				byteSize--;
			}
			if (byteSize <= byteLength - 2) {
				break;
			}
		}

		return src.substring(0, index) + "..";
	}

	public static String formatSeconds(int duration) {
		StringBuilder sb = new StringBuilder();
		duration = duration / 1000;
		int mins = duration / 60;
		int hours = 0;
		if (mins > 60) {
			hours = mins / 60;
			mins = mins % 60;
		}
		int seconds = duration % 60;
		if (hours != 0) {
			sb.append(hours).append(":");
		}
		if (mins != 0) {
			if (mins < 10 && hours != 0) {
				sb.append("0");
			}
			sb.append(mins).append(":");
		} else {
			if (hours != 0) {
				sb.append("00").append(":");
			} else {
				sb.append("0").append(":");
			}
		}
		if (seconds != 0) {
			if (seconds < 10) {
				sb.append("0");
			}
			sb.append(seconds);
		} else {
			sb.append("00");
		}
		return sb.toString();
	}

	/**
	 * judge the char is wide byte char
	 * 
	 * @param ch
	 * @return
	 */
	public static boolean isWideByteChar(char ch) {
		return ch >= 0x80;
	}

	public static String formatEnter(String str) {
		return str.replace("\r\n", "<br>");
	}

	public static String deformatEnter(String str) {
		return str.replace("<br>", "\r\n");
	}

	/**
	 * 
	 * @param template
	 * @param values
	 * @return
	 */
	public static<K, V> String replacePlaceHolder(String template, Map<K, V> valueMap) {

		if (template.indexOf("${") == -1) {
			return template;
		}

		while (true) {
			int start = template.indexOf("${");
			int end = template.indexOf("}", start);

			if (start != -1 && end != -1) {
				String temp = template.substring(start + 2, end);

				if (valueMap.keySet().contains(temp)) { // contains the dynamic
														// string then replace
					template = template.substring(0, start)
							+ (String) valueMap.get(temp)
							+ template.substring(end + 1);
				} else {
					template = template.substring(0, start)
							+ template.substring(end + 1);
				}

			} else {
				break;
			}
		}

		return template;
	}

	/**
	 * 
	 * @param template
	 * @return
	 */
	public static boolean hasPlaceHolder(String template) {
		if (template.indexOf("${") == -1) {
			return false;
		} else {
			return true;
		}
	}

	public static List<String> getStringListByString(String str) {
		if (str != null) {
			List<String> list = new ArrayList<String>();
			String[] strs = str.split(",");
			for (String s : strs) {
				list.add(s);
			}
			return list;
		}
		return null;
	}

	public static<T> String getStringByList(List<T> list) {
		if (list == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (Object o : list) {
			sb.append(String.valueOf(o)).append(",");
		}
		if (sb.length() > 0) {
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}

	public static List<Integer> getIntegerListByString(String str) {
		if (str != null) {
			List<Integer> list = new ArrayList<Integer>();
			String[] strs = str.split(",");
			for (String s : strs) {
				list.add(new Integer(s));
			}
			return list;
		}
		return null;
	}

	public static boolean containsValue(String[] arr, String v) {
		if (v == null || arr == null) {
			return false;
		}
		for (String s : arr) {
			if (v.equals(s)) {
				return true;
			}
		}
		return false;
	}

    /**
     * Connect strings together
     *
     * @param parts
     * @return
     */
    public static String connect(String... parts) {
        StringBuilder sb = new StringBuilder();
        for (String part : parts) {
            sb.append(part);
        }
        return sb.toString();
    }

    /**
     * Convert camel naming to any naming using seperator
     *
     * @param template
     * @param seperator
     * @return
     */
    public static String convertCamel(String template, String seperator) {
        StringBuffer result = new StringBuffer();
        if (template != null && template.length() > 0) {
            result.append(template.substring(0, 1).toLowerCase());
            for (int i = 1; i < template.length(); i++) {
                String s = template.substring(i, i + 1);
                if (s.equals(s.toUpperCase()) && !s.equals(seperator)) {
                    result.append(seperator);
                    result.append(s.toLowerCase());
                } else {
                    result.append(s);
                }
            }
        }
        return result.toString();
    }

    /**
     * 
     * @param string
     * @param length
     * @return
     */
    public static String left(String string, int length) {
        return org.apache.commons.lang.StringUtils.left(string, length);
    }

}
